package com.aegean.CommercialHotelBookSystem.user.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.aegean.CommercialHotelBookSystem.model.Customer;

public class Login_Filter implements Filter {
	FilterConfig config = null;
	String webroot;

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpSession session = request.getSession();
		String uri = request.getRequestURI();
		if (uri != null
				&& (uri.equals(webroot + "/User/Login_User.action")
						|| uri.equals(webroot + "/User/User_login.jsp")
						|| uri.equals(webroot + "/User/Exception_User.jsp")
						|| uri.equals(webroot + "/User/User_insert.jsp") || uri
						.equals(webroot + "/User/Add_User.action")||uri
						.equals(webroot + "/User/User_search.action"))) {
			chain.doFilter(req, resp);
		} else {
			if (session == null) {
				response.sendRedirect(webroot + "/User/User_login.jsp");
			} else {
				Customer one = (Customer) session.getAttribute("Login_User");
				if (one == null) {
					response.sendRedirect(webroot + "/User/User_login.jsp");
				} else {
					chain.doFilter(req, resp);
				}
			}
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
		this.config = fConfig;
		webroot = config.getServletContext().getContextPath();
	}

	public void destroy() {

	}

}
